/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.controller

import android.car.VehicleAreaType
import android.car.hardware.property.CarPropertyManager
import com.ecarx.systemui.plugin.model.centerlock.CenterLockImpl

class CenterLockController private constructor() {
    private val mCenterLock = CenterLockImpl()
    private val mArea = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL

    suspend fun registerCallback(callback: CarPropertyManager.CarPropertyEventCallback) {
        if (!isCenterLockAvailable()) {
            return
        }
        mCenterLock.registerCallback(callback)
    }

    suspend fun unRegisterCallback(callback: CarPropertyManager.CarPropertyEventCallback) {
        if (!isCenterLockAvailable()) {
            return
        }
        mCenterLock.unRegisterCallback(callback)
    }

    suspend fun isCenterLockAvailable(): Boolean {
        return mCenterLock.isAvailable(mArea)
    }

    suspend fun getCenterLockState(): Boolean {
        if (isCenterLockAvailable()) {
            return mCenterLock.getValue(mArea)
        }
        return false
    }

    suspend fun setCenterLock(value: Boolean) {
        if (isCenterLockAvailable()) {
            mCenterLock.setValue(value, mArea)
        }
    }

    companion object {
        private const val TAG = "CenterLockController"
        fun getInstance() = Holder.instance
    }

    private object Holder {
        val instance = CenterLockController()
    }
}